package com.fangcun.service.admin.Impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.fangcun.bean.admin.goods.GoodsResponse;
import com.fangcun.bean.admin.goods.response.GoodsDetailResponse;
import com.fangcun.bean.sys.PageBean;
import com.fangcun.common.canstant.ErrCode;
import com.fangcun.common.exception.ProException;
import com.fangcun.common.exception.ProValiDataException;
import com.fangcun.common.util.qiniu.QiniuUtils;
import com.fangcun.entity.Goods;
import com.fangcun.entity.GoodsPrice;
import com.fangcun.entity.User;
import com.fangcun.mapper.GoodsMapper;
import com.fangcun.mapper.GoodsPriceMapper;
import com.fangcun.mapper.UserMapper;
import com.fangcun.service.admin.GoodsSeriver;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class GoodsSeriverImpl implements GoodsSeriver {
	@Autowired
	private GoodsMapper goodsMapper;
	@Autowired
	private QiniuUtils qiniuUtils;
	@Autowired
	private GoodsPriceMapper goodsPriceMapper;
	@Autowired
	private UserMapper userMapper;

	@Override
	public PageInfo<GoodsResponse> list(PageBean<Goods> request) throws ProException {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<GoodsResponse> list = goodsMapper.listForAdmin(request.getParam());
		if (list == null || list.size() == 0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return new PageInfo<>(list);
	}

	@Override
	public PageInfo<GoodsPrice> listPrice(PageBean<GoodsPrice> request) throws ProException {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<GoodsPrice> list = goodsPriceMapper.listForAdmin(request.getParam());
		if (list == null || list.size() == 0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return new PageInfo<>(list);
	}

	@Override
	public Long add(Goods request) throws ProException {
		if (!StringUtils.isEmpty(request.getListLogo())) {
			request.setListLogo(qiniuUtils.uploadImageUrl(request.getListLogo()));
		}
		if (!StringUtils.isEmpty(request.getDetailBanner())) {
			request.setDetailBanner(qiniuUtils.uploadImageUrl(request.getDetailBanner()));
		}
		request.setIntroduce(qiniuUtils.dealHtml(request.getIntroduce(), "1/" + request.getTeahouseId() + "/"));
		if (goodsMapper.insertSelective(request) == 0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return request.getId();
	}

	@Override
	public void addPrice(GoodsPrice request) throws ProException {
		if (!StringUtils.isEmpty(request.getPreviewImage())) {
			request.setPreviewImage(qiniuUtils.uploadImageUrl(request.getPreviewImage()));
		}
		try {
			if (goodsPriceMapper.insertSelective(request) == 0) {
				throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
			}
		} catch (DataAccessException e) {
			throw new ProValiDataException(ErrCode.ERR_HAVE_EXISTED.getReturnCode(), ErrCode.ERR_HAVE_EXISTED.getReturnMsg());
		}
	}

	@Override
	public GoodsDetailResponse detail(Long id) throws ProException {
		GoodsDetailResponse request = goodsMapper.detailForAdmin(id);
		if (request== null) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return request;
	}

	@Override
	public GoodsPrice detailsPrice(Long id) throws ProException {
		GoodsPrice request = goodsPriceMapper.selectByPrimaryKey(id);
		if (request == null) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return request;
	}

	@Override
	public void update(Goods request) throws ProException {
		if (!StringUtils.isEmpty(request.getListLogo())) {
			request.setListLogo(qiniuUtils.uploadImageUrl(request.getListLogo()));
		}
		if (!StringUtils.isEmpty(request.getDetailBanner())) {
			request.setDetailBanner(qiniuUtils.uploadImageUrl(request.getDetailBanner()));
		}
		request.setIntroduce(qiniuUtils.dealHtml(request.getIntroduce(), "1/" + request.getTeahouseId() + "/"));
		if (goodsMapper.updateByPrimaryKeySelective(request) == 0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}

	@Override
	public void updatePrice(GoodsPrice request) throws ProException {
		if (!StringUtils.isEmpty(request.getPreviewImage())) {
			request.setPreviewImage(qiniuUtils.uploadImageUrl(request.getPreviewImage()));
		}
		if (goodsPriceMapper.updateByPrimaryKeySelective(request) == 0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}

	@Override
	public List<User> userDropDown(User request) throws ProException {
		List<User> list = userMapper.selectUser(request);
		if (list == null || list.size() == 0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return list;
	}

	@Override
	public void updateListPrice(List<GoodsPrice> request) throws ProException {
		for (GoodsPrice goodsPrice : request) {
			if (goodsPriceMapper.updateByPrimaryKeySelective(goodsPrice) == 0) {
				throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
			}
		}
	}
}
